package br.net.woodstock.tickettoride.vaadin.web.security;

import br.net.woodstock.tickettoride.vaadin.web.AbstractView;
import br.net.woodstock.tickettoride.vaadin.web.TicketToRideApplication;

import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Panel;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;

public class LogonView extends AbstractView {

	private static final long	serialVersionUID	= 6359141818451948198L;

	private TextField			login;

	private PasswordField		password;

	private Button				loginButton;

	public LogonView() {
		super();
	}

	@Override
	protected void init() {
		Panel panel = new Panel();

		this.login = new TextField(this.getMessage("label.login"));
		this.login.setMaxLength(20);
		this.login.setRequired(true);

		this.password = new PasswordField(this.getMessage("label.password"));
		this.password.setMaxLength(20);
		this.password.setRequired(true);

		this.loginButton = new Button(this.getMessage("label.login"));

		panel.addComponent(this.login);
		panel.addComponent(this.password);
		panel.addComponent(this.loginButton);
		panel.setWidth(250, Sizeable.UNITS_PIXELS);

		this.loginButton.addListener(new ClickListener() {

			private static final long	serialVersionUID	= 1L;

			@Override
			public void buttonClick(final ClickEvent event) {
				TicketToRideApplication application = (TicketToRideApplication) LogonView.this.getApplication();

				String login = (String) LogonView.this.getLogin().getValue();
				String password = (String) LogonView.this.getPassword().getValue();

				application.executeLogin(login, password);
			}

		});

		this.addComponent(panel);
		this.setComponentAlignment(panel, Alignment.TOP_CENTER);
	}

	public TextField getLogin() {
		return this.login;
	}

	public PasswordField getPassword() {
		return this.password;
	}

	public Button getLoginButton() {
		return this.loginButton;
	}

}
